package org.flhy.ext.trans.steps;

import java.util.List;

import org.flhy.ext.core.PropsUI;
import org.flhy.ext.trans.step.AbstractStep;
import org.flhy.ext.utils.JSONArray;
import org.flhy.ext.utils.JSONObject;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.di.trans.steps.http.HTTPMeta;
import org.pentaho.metastore.api.IMetaStore;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxUtils;
/**
 * Created by wujinhui 2018/12/6
 */
@Component("HTTP")
@Scope("prototype")
public class HTTP extends AbstractStep {


    @Override
    public void decode(StepMetaInterface stepMetaInterface, mxCell cell, List<DatabaseMeta> databases, IMetaStore metaStore) throws Exception {
        HTTPMeta httpMeta = (HTTPMeta) stepMetaInterface;
        httpMeta.setSocketTimeout(cell.getAttribute("socketTimeout"));
        httpMeta.setConnectionTimeout(cell.getAttribute("socketTimeout"));
        httpMeta.setCloseIdleConnectionsTime(cell.getAttribute("closeIdleConnectionsTime"));
        httpMeta.setUrl(cell.getAttribute("url"));
        httpMeta.setFieldName(cell.getAttribute("fieldName"));
        httpMeta.setEncoding(cell.getAttribute("encoding"));
        httpMeta.setUrlInField(cell.getAttribute("urlInField").equals("true")?true:false);
        httpMeta.setUrlField(cell.getAttribute("urlInField"));
        httpMeta.setProxyHost(cell.getAttribute("proxyHost"));
        httpMeta.setProxyPort(cell.getAttribute("proxyPort"));
        httpMeta.setHttpLogin(cell.getAttribute("httpLogin"));
        httpMeta.setHttpPassword(cell.getAttribute("httpPassword"));
        httpMeta.setResultCodeFieldName(cell.getAttribute("resultCodeFieldName"));
        httpMeta.setResponseHeaderFieldName(cell.getAttribute("responseHeaderFieldName"));

        //对应数组的处理
        JSONArray jsonArray = JSONArray.fromObject(cell.getAttribute("arguments"));
        String[] argumentFields = new String[jsonArray.size()];
        String[] argumentParameters= new String[jsonArray.size()];
        for( int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            argumentFields[i]=jsonObject.optString("argumentField");
            argumentParameters[i]=jsonObject.optString("argumentParameter");
        }
        httpMeta.setArgumentField(argumentFields);
        httpMeta.setArgumentParameter(argumentParameters);

        JSONArray jsonArray2 = JSONArray.fromObject(cell.getAttribute("headers"));
        String[] headerParameters = new String[jsonArray2.size()];
        String[] headerFields= new String[jsonArray2.size()];
        for( int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            headerParameters[i]=jsonObject.optString("headerParameter");
            headerFields[i]=jsonObject.optString("headerField");
        }
        httpMeta.setHeaderParameter(headerParameters);
        httpMeta.setHeaderField(headerFields);


    }

    @Override
    public Element encode(StepMetaInterface stepMetaInterface) throws Exception {

        HTTPMeta httpMeta=(HTTPMeta) stepMetaInterface;
        Document doc = mxUtils.createDocument();
        Element e = doc.createElement(PropsUI.TRANS_STEP_NAME);
       e.setAttribute("socketTimeout",httpMeta.getSocketTimeout());
       e.setAttribute("connectionTimeout",httpMeta.getConnectionTimeout());
       e.setAttribute("closeIdleConnectionsTime",httpMeta.getCloseIdleConnectionsTime());
       e.setAttribute("url",httpMeta.getUrl());
       e.setAttribute("fieldName",httpMeta.getFieldName());
       e.setAttribute("encoding",httpMeta.getEncoding());
       e.setAttribute("urlInField",httpMeta.isUrlInField()==true?"true":"false");
       e.setAttribute("urlField",httpMeta.getUrlField());
       e.setAttribute("proxyHost",httpMeta.getProxyHost());
       e.setAttribute("proxyPort",httpMeta.getProxyPort());
       e.setAttribute("httpLogin",httpMeta.getHttpLogin());
        e.setAttribute("httpPassword",httpMeta.getHttpPassword());
        e.setAttribute("resultCodeFieldName",httpMeta.getResultCodeFieldName());
        e.setAttribute("responseTimeFieldName",httpMeta.getResponseTimeFieldName());
        e.setAttribute("responseHeaderFieldName",httpMeta.getResponseHeaderFieldName());

        //对数组的处理
          //argument
        JSONArray jsonArray = new JSONArray();
        String[] argumentFields = httpMeta.getArgumentField();
        String[] argumentParameters=httpMeta.getArgumentParameter();
        for ( int i = 0; i < argumentFields.length; i++ ) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("argumentField", argumentFields[i]);
            jsonObject.put("argumentParameter",argumentParameters[i]);
            jsonArray.add(jsonObject);
        }
        e.setAttribute("arguments", jsonArray.toString());
         //header
        jsonArray = new JSONArray();
        String[] headerParameters = httpMeta.getHeaderParameter();
        String[] headerFields=httpMeta.getHeaderField();
        for ( int i = 0; i < headerParameters.length; i++ ) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("headerParameter", headerParameters[i]);
            jsonObject.put("headerField", headerFields[i]);
            jsonArray.add(jsonObject);
        }
        e.setAttribute("headers", jsonArray.toString());

        return e;
    }
}
